using System;
using System.Data;
using System.Data.OleDb;
using PCSComUtils.Common;
using PCSComUtils.DataAccess;

using PCSComUtils.PCSExc;

namespace PCSComPayroll.DS
{
	public class PAY_AdditionSalaryMasterDS
	{
		public PAY_AdditionSalaryMasterDS()
		{
		}

		private const string THIS = "PCSComPayroll.DS.DS.PAY_AdditionSalaryMasterDS";

		public void Add(object pobjObjectVO)
		{
			const string METHOD_NAME = THIS + ".Add()";

			OleDbConnection oconPCS = null;
			OleDbCommand ocmdPCS = null;
			try
			{
				PAY_AdditionSalaryMasterVO objObject = (PAY_AdditionSalaryMasterVO) pobjObjectVO;
				string strSql = String.Empty;
				
				oconPCS = new OleDbConnection(Utils.Instance.OleDbConnectionString);
				ocmdPCS = new OleDbCommand("", oconPCS);

				strSql = "INSERT INTO PAY_AdditionSalaryMaster("
					+ PAY_AdditionSalaryMasterTable.TRANSNO_FLD + ","
					+ PAY_AdditionSalaryMasterTable.POSTDATE_FLD + ","
					+ PAY_AdditionSalaryMasterTable.PRODUCTIONLINEID_FLD + ","
					+ PAY_AdditionSalaryMasterTable.SALEORDERDETAILID_FLD + ")"
					+ "VALUES(?,?,?,?)";

				ocmdPCS.Parameters.Add(new OleDbParameter(PAY_AdditionSalaryMasterTable.TRANSNO_FLD, OleDbType.WChar));
				ocmdPCS.Parameters[PAY_AdditionSalaryMasterTable.TRANSNO_FLD].Value = objObject.TransNo;

				ocmdPCS.Parameters.Add(new OleDbParameter(PAY_AdditionSalaryMasterTable.POSTDATE_FLD, OleDbType.Date));
				ocmdPCS.Parameters[PAY_AdditionSalaryMasterTable.POSTDATE_FLD].Value = objObject.PostDate;

				ocmdPCS.Parameters.Add(new OleDbParameter(PAY_AdditionSalaryMasterTable.PRODUCTIONLINEID_FLD, OleDbType.Integer));
				ocmdPCS.Parameters[PAY_AdditionSalaryMasterTable.PRODUCTIONLINEID_FLD].Value = objObject.ProductionLineID;

				ocmdPCS.Parameters.Add(new OleDbParameter(PAY_AdditionSalaryMasterTable.SALEORDERDETAILID_FLD, OleDbType.Integer));
				ocmdPCS.Parameters[PAY_AdditionSalaryMasterTable.SALEORDERDETAILID_FLD].Value = objObject.SaleOrderDetailID;


				ocmdPCS.CommandText = strSql;
				ocmdPCS.Connection.Open();
				ocmdPCS.ExecuteNonQuery();

			}
			catch (OleDbException ex)
			{
				throw new PCSDBException(ErrorCode.ERROR_DB, METHOD_NAME, ex);
			}

			catch (InvalidOperationException ex)
			{
				throw new PCSDBException(ErrorCode.DUPLICATE_KEY, METHOD_NAME, ex);
			}
			catch (Exception ex)
			{
				throw new PCSDBException(ErrorCode.OTHER_ERROR, METHOD_NAME, ex);
			}

			finally
			{
				if (oconPCS != null)
				{
					if (oconPCS.State != ConnectionState.Closed)
					{
						oconPCS.Close();
					}
				}
			}
		}

		public int AddAndReturnID(object pobjObjectVO)
		{
			const string METHOD_NAME = THIS + ".AddAndReturnID()";

			OleDbConnection oconPCS = null;
			OleDbCommand ocmdPCS = null;
			try
			{
				PAY_AdditionSalaryMasterVO objObject = (PAY_AdditionSalaryMasterVO) pobjObjectVO;
				string strSql = String.Empty;
				
				oconPCS = new OleDbConnection(Utils.Instance.OleDbConnectionString);
				ocmdPCS = new OleDbCommand("", oconPCS);

				strSql = "INSERT INTO PAY_AdditionSalaryMaster("
					+ PAY_AdditionSalaryMasterTable.TRANSNO_FLD + ","
					+ PAY_AdditionSalaryMasterTable.POSTDATE_FLD + ","
					+ PAY_AdditionSalaryMasterTable.PRODUCTIONLINEID_FLD + ","
					+ PAY_AdditionSalaryMasterTable.SALEORDERDETAILID_FLD + ")"
					+ "VALUES(?,?,?,?)"
					+ "; SELECT @@IDENTITY as LatestID";

				ocmdPCS.Parameters.Add(new OleDbParameter(PAY_AdditionSalaryMasterTable.TRANSNO_FLD, OleDbType.WChar));
				ocmdPCS.Parameters[PAY_AdditionSalaryMasterTable.TRANSNO_FLD].Value = objObject.TransNo;

				ocmdPCS.Parameters.Add(new OleDbParameter(PAY_AdditionSalaryMasterTable.POSTDATE_FLD, OleDbType.Date));
				ocmdPCS.Parameters[PAY_AdditionSalaryMasterTable.POSTDATE_FLD].Value = objObject.PostDate;

				ocmdPCS.Parameters.Add(new OleDbParameter(PAY_AdditionSalaryMasterTable.PRODUCTIONLINEID_FLD, OleDbType.Integer));
				ocmdPCS.Parameters[PAY_AdditionSalaryMasterTable.PRODUCTIONLINEID_FLD].Value = objObject.ProductionLineID;

				ocmdPCS.Parameters.Add(new OleDbParameter(PAY_AdditionSalaryMasterTable.SALEORDERDETAILID_FLD, OleDbType.Integer));
				ocmdPCS.Parameters[PAY_AdditionSalaryMasterTable.SALEORDERDETAILID_FLD].Value = objObject.SaleOrderDetailID;


				ocmdPCS.CommandText = strSql;
				ocmdPCS.Connection.Open();
				return Convert.ToInt32(ocmdPCS.ExecuteScalar());
			}
			catch (OleDbException ex)
			{
				throw new PCSDBException(ErrorCode.ERROR_DB, METHOD_NAME, ex);
			}

			catch (InvalidOperationException ex)
			{
				throw new PCSDBException(ErrorCode.DUPLICATE_KEY, METHOD_NAME, ex);
			}
			catch (Exception ex)
			{
				throw new PCSDBException(ErrorCode.OTHER_ERROR, METHOD_NAME, ex);
			}

			finally
			{
				if (oconPCS != null)
				{
					if (oconPCS.State != ConnectionState.Closed)
					{
						oconPCS.Close();
					}
				}
			}
		}

		public void Delete(int pintID)
		{
			const string METHOD_NAME = THIS + ".Delete()";
			string strSql = String.Empty;
			strSql = "DELETE " + PAY_AdditionSalaryMasterTable.TABLE_NAME + " WHERE  " + "AdditionSalaryMasterID" + "=" + pintID.ToString();
			OleDbConnection oconPCS = null;
			OleDbCommand ocmdPCS = null;
			try
			{
				
				oconPCS = new OleDbConnection(Utils.Instance.OleDbConnectionString);
				ocmdPCS = new OleDbCommand(strSql, oconPCS);

				ocmdPCS.Connection.Open();
				ocmdPCS.ExecuteNonQuery();
				ocmdPCS = null;

			}
			catch (OleDbException ex)
			{
				throw new PCSDBException(ErrorCode.ERROR_DB, METHOD_NAME, ex);
			}

			catch (Exception ex)
			{
				throw new PCSDBException(ErrorCode.OTHER_ERROR, METHOD_NAME, ex);
			}


			finally
			{
				if (oconPCS != null)
				{
					if (oconPCS.State != ConnectionState.Closed)
					{
						oconPCS.Close();
					}
				}
			}
		}

		public object GetObjectVO(int pintID)
		{
			const string METHOD_NAME = THIS + ".GetObjectVO()";
			DataSet dstPCS = new DataSet();

			OleDbDataReader odrPCS = null;
			OleDbConnection oconPCS = null;
			OleDbCommand ocmdPCS = null;
			try
			{
				string strSql = String.Empty;
				strSql = "SELECT "
					+ PAY_AdditionSalaryMasterTable.ADDITIONSALARYMASTERID_FLD + ","
					+ PAY_AdditionSalaryMasterTable.TRANSNO_FLD + ","
					+ PAY_AdditionSalaryMasterTable.POSTDATE_FLD + ","
					+ PAY_AdditionSalaryMasterTable.PRODUCTIONLINEID_FLD + ","
					+ PAY_AdditionSalaryMasterTable.SALEORDERDETAILID_FLD
					+ " FROM " + PAY_AdditionSalaryMasterTable.TABLE_NAME
					+ " WHERE " + PAY_AdditionSalaryMasterTable.ADDITIONSALARYMASTERID_FLD + "=" + pintID;

				
				oconPCS = new OleDbConnection(Utils.Instance.OleDbConnectionString);
				ocmdPCS = new OleDbCommand(strSql, oconPCS);

				ocmdPCS.Connection.Open();
				odrPCS = ocmdPCS.ExecuteReader();

				PAY_AdditionSalaryMasterVO objObject = new PAY_AdditionSalaryMasterVO();

				while (odrPCS.Read())
				{
					objObject.AdditionSalaryMasterID = int.Parse(odrPCS[PAY_AdditionSalaryMasterTable.ADDITIONSALARYMASTERID_FLD].ToString());
					objObject.TransNo = odrPCS[PAY_AdditionSalaryMasterTable.TRANSNO_FLD].ToString();
					objObject.PostDate = DateTime.Parse(odrPCS[PAY_AdditionSalaryMasterTable.POSTDATE_FLD].ToString());
					objObject.ProductionLineID = int.Parse(odrPCS[PAY_AdditionSalaryMasterTable.PRODUCTIONLINEID_FLD].ToString());
					objObject.SaleOrderDetailID = int.Parse(odrPCS[PAY_AdditionSalaryMasterTable.SALEORDERDETAILID_FLD].ToString());

				}
				return objObject;
			}
			catch (OleDbException ex)
			{
				throw new PCSDBException(ErrorCode.ERROR_DB, METHOD_NAME, ex);
			}

			catch (Exception ex)
			{
				throw new PCSDBException(ErrorCode.OTHER_ERROR, METHOD_NAME, ex);
			}

			finally
			{
				if (oconPCS != null)
				{
					if (oconPCS.State != ConnectionState.Closed)
					{
						oconPCS.Close();
					}
				}
			}
		}

		public void Update(object pobjObjecVO)
		{
			const string METHOD_NAME = THIS + ".Update()";

			PAY_AdditionSalaryMasterVO objObject = (PAY_AdditionSalaryMasterVO) pobjObjecVO;


			//prepare value for parameters
			OleDbConnection oconPCS = null;
			OleDbCommand ocmdPCS = null;
			try
			{
				string strSql = String.Empty;
				
				oconPCS = new OleDbConnection(Utils.Instance.OleDbConnectionString);
				ocmdPCS = new OleDbCommand(strSql, oconPCS);
				strSql = "UPDATE PAY_AdditionSalaryMaster SET "
					+ PAY_AdditionSalaryMasterTable.TRANSNO_FLD + "=   ?" + ","
					+ PAY_AdditionSalaryMasterTable.POSTDATE_FLD + "=   ?" + ","
					+ PAY_AdditionSalaryMasterTable.PRODUCTIONLINEID_FLD + "=   ?" + ","
					+ PAY_AdditionSalaryMasterTable.SALEORDERDETAILID_FLD + "=  ?"
					+ " WHERE " + PAY_AdditionSalaryMasterTable.ADDITIONSALARYMASTERID_FLD + "= ?";

				ocmdPCS.Parameters.Add(new OleDbParameter(PAY_AdditionSalaryMasterTable.TRANSNO_FLD, OleDbType.WChar));
				ocmdPCS.Parameters[PAY_AdditionSalaryMasterTable.TRANSNO_FLD].Value = objObject.TransNo;

				ocmdPCS.Parameters.Add(new OleDbParameter(PAY_AdditionSalaryMasterTable.POSTDATE_FLD, OleDbType.Date));
				ocmdPCS.Parameters[PAY_AdditionSalaryMasterTable.POSTDATE_FLD].Value = objObject.PostDate;

				ocmdPCS.Parameters.Add(new OleDbParameter(PAY_AdditionSalaryMasterTable.PRODUCTIONLINEID_FLD, OleDbType.Integer));
				ocmdPCS.Parameters[PAY_AdditionSalaryMasterTable.PRODUCTIONLINEID_FLD].Value = objObject.ProductionLineID;

				ocmdPCS.Parameters.Add(new OleDbParameter(PAY_AdditionSalaryMasterTable.SALEORDERDETAILID_FLD, OleDbType.Integer));
				ocmdPCS.Parameters[PAY_AdditionSalaryMasterTable.SALEORDERDETAILID_FLD].Value = objObject.SaleOrderDetailID;

				ocmdPCS.Parameters.Add(new OleDbParameter(PAY_AdditionSalaryMasterTable.ADDITIONSALARYMASTERID_FLD, OleDbType.Integer));
				ocmdPCS.Parameters[PAY_AdditionSalaryMasterTable.ADDITIONSALARYMASTERID_FLD].Value = objObject.AdditionSalaryMasterID;


				ocmdPCS.CommandText = strSql;
				ocmdPCS.Connection.Open();
				ocmdPCS.ExecuteNonQuery();
			}
			catch (OleDbException ex)
			{
				throw new PCSDBException(ErrorCode.ERROR_DB, METHOD_NAME, ex);
			}

			catch (InvalidOperationException ex)
			{
				throw new PCSDBException(ErrorCode.DUPLICATE_KEY, METHOD_NAME, ex);
			}
			catch (Exception ex)
			{
				throw new PCSDBException(ErrorCode.OTHER_ERROR, METHOD_NAME, ex);
			}

			finally
			{
				if (oconPCS != null)
				{
					if (oconPCS.State != ConnectionState.Closed)
					{
						oconPCS.Close();
					}
				}
			}

		}

		public DataSet List()
		{
			const string METHOD_NAME = THIS + ".List()";
			DataSet dstPCS = new DataSet();


			OleDbConnection oconPCS = null;
			OleDbCommand ocmdPCS = null;
			try
			{
				string strSql = String.Empty;

				strSql = "SELECT "
					+ PAY_AdditionSalaryMasterTable.ADDITIONSALARYMASTERID_FLD + ","
					+ PAY_AdditionSalaryMasterTable.TRANSNO_FLD + ","
					+ PAY_AdditionSalaryMasterTable.POSTDATE_FLD + ","
					+ PAY_AdditionSalaryMasterTable.PRODUCTIONLINEID_FLD + ","
					+ PAY_AdditionSalaryMasterTable.SALEORDERDETAILID_FLD
					+ " FROM " + PAY_AdditionSalaryMasterTable.TABLE_NAME;
				
				oconPCS = new OleDbConnection(Utils.Instance.OleDbConnectionString);
				ocmdPCS = new OleDbCommand(strSql, oconPCS);
				ocmdPCS.Connection.Open();

				OleDbDataAdapter odadPCS = new OleDbDataAdapter(ocmdPCS);
				odadPCS.Fill(dstPCS, PAY_AdditionSalaryMasterTable.TABLE_NAME);

				return dstPCS;
			}
			catch (OleDbException ex)
			{
				throw new PCSDBException(ErrorCode.ERROR_DB, METHOD_NAME, ex);
			}
			catch (Exception ex)
			{
				throw new PCSDBException(ErrorCode.OTHER_ERROR, METHOD_NAME, ex);
			}

			finally
			{
				if (oconPCS != null)
				{
					if (oconPCS.State != ConnectionState.Closed)
					{
						oconPCS.Close();
					}
				}
			}


		}

		public void UpdateDataSet(DataSet pData)
		{
			const string METHOD_NAME = THIS + ".UpdateDataSet()";
			string strSql;
			OleDbConnection oconPCS = null;
			OleDbCommandBuilder odcbPCS;
			OleDbDataAdapter odadPCS = new OleDbDataAdapter();

			try
			{
				strSql = "SELECT "
					+ PAY_AdditionSalaryMasterTable.ADDITIONSALARYMASTERID_FLD + ","
					+ PAY_AdditionSalaryMasterTable.TRANSNO_FLD + ","
					+ PAY_AdditionSalaryMasterTable.POSTDATE_FLD + ","
					+ PAY_AdditionSalaryMasterTable.PRODUCTIONLINEID_FLD + ","
					+ PAY_AdditionSalaryMasterTable.SALEORDERDETAILID_FLD
					+ " FROM " + PAY_AdditionSalaryMasterTable.TABLE_NAME;


				
				oconPCS = new OleDbConnection(Utils.Instance.OleDbConnectionString);
				odadPCS.SelectCommand = new OleDbCommand(strSql, oconPCS);
				odcbPCS = new OleDbCommandBuilder(odadPCS);
				pData.EnforceConstraints = false;
				odadPCS.Update(pData, PAY_AdditionSalaryMasterTable.TABLE_NAME);

			}
			catch (OleDbException ex)
			{
				throw new PCSDBException(ErrorCode.ERROR_DB, METHOD_NAME, ex);
			}

			catch (InvalidOperationException ex)
			{
				throw new PCSDBException(ErrorCode.DUPLICATE_KEY, METHOD_NAME, ex);
			}
			catch (Exception ex)
			{
				throw new PCSDBException(ErrorCode.OTHER_ERROR, METHOD_NAME, ex);
			}

			finally
			{
				if (oconPCS != null)
				{
					if (oconPCS.State != ConnectionState.Closed)
					{
						oconPCS.Close();
					}
				}
			}

		}
	}
}